<?php


namespace app\api\model\User;


use think\facade\Db;
use think\Model;

class UserWatchLogModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "user_watch_log";

    /**
     * 根据条件查询单挑数据
     * @param $where
     * @param string $filed
     * @return array|Model|null
     * @throws
     */
    public static function getUserWatchLogWhere($where,$filed = "*")
    {
        $bill = Db::name("user_watch_log")->field($filed)->where($where)->find();
        return $bill;
    }

    /**
     * @param $where
     * @param string $filed
     * @return array|Model|null
     * @throws
     */
    public static function getUserWatchLogWhereJoin($where,$filed = "uwb.*")
    {
        $user = Db::name("user_watch_log")
            ->alias('uwb')
            ->join('user u','u.id = uwb.uid')
            ->field($filed)
            ->where($where)->find();
        return $user;
    }

    /**
     * 根据条件查询多条数据
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getUserWatchLogWhereMore($where)
    {
        $list = Db::name("user_watch_log")->where($where)->select()->toArray();
        return $list;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getUserWatchLogCountWhere($where)
    {
        $count = Db::name("user_watch_log")
            ->alias('uwl')
            ->join('video v','v.id = uwl.video_id')
            ->where($where)->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param array $where
     * @param string $field
     * @return array
     * @throws
     */
    public static function getUserWatchLogListWhere($page,$limit,$where = [],$field="v.*")
    {
        $start = ceil(($page-1)*$limit);
        $list = Db::name('user_watch_log')
            ->alias('uwl')
            ->join('video v','v.id = uwl.video_id')
            ->field($field)
            ->where($where)
            ->limit($start,$limit)
            ->select()->toArray();
        return $list;
    }

    /**
     * 添加
     * @param $data
     * @return int|string
     */
    public static function addUserWatchLog($data)
    {
        $id = Db::name('user_watch_log')->insertGetId($data);
        return $id;
    }

    /**
     * 更新
     * @param $where
     * @param $save
     * @return int
     */
    public static function updateUserWatchLog($where,$save)
    {
        $flag = Db::name('user_watch_log')->where($where)->save($save);
        return $flag;
    }

}